package free.emaranhado.casadolago.requests;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;
import free.emaranhado.casadolago.listener.XMLParserListener;
import free.emaranhado.casadolago.parser.CategoriesXMLParser;
import free.emaranhado.casadolago.parser.EventsXMLParser;

public class ApiRequests {
	private Context mContext;
	
	public ApiRequests(Context context) {
		mContext = context;
	}

	public void requestEvents(String idCategory, XMLParserListener listener) {

		try {
			/** Handling XML */
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser spXml = spf.newSAXParser();
			XMLReader xmlr = spXml.getXMLReader();

			AssetManager assetManager = mContext.getAssets();
			InputStream inputStream = null;
			try {
				inputStream = assetManager.open("events.xml");
			} catch (IOException e) {
				Log.e("URL", "IOException");
			}

			EventsXMLParser XMLParser = new EventsXMLParser();
			XMLParser.setListener(listener);
			xmlr.setContentHandler(XMLParser);
			xmlr.parse(new InputSource(inputStream));

		} catch (Exception e) {
			System.out.println("XML Pasing Excpetion = " + e);
		}
	}
	
	public void requestCategories(XMLParserListener listener) {
		try {
			/** Handling XML */
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser spXml = spf.newSAXParser();
			XMLReader xmlr = spXml.getXMLReader();

			AssetManager assetManager = mContext.getAssets();
			InputStream inputStream = null;
			try {
				inputStream = assetManager.open("categories.xml");
			} catch (IOException e) {
				Log.e("URL", "IOException");
			}

			CategoriesXMLParser XMLParser = new CategoriesXMLParser();
			XMLParser.setListener(listener);
			xmlr.setContentHandler(XMLParser);
			xmlr.parse(new InputSource(inputStream));

		} catch (Exception e) {
			System.out.println("XML Pasing Excpetion = " + e);
		}
	
	}
	
	
	
}
